package com.Poker.Group;

import com.Poker.Poker;
import edu.emory.mathcs.backport.java.util.Collections;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mike on 18/5/23.
 *
 */
public abstract class BaseGroupFilter implements GroupFilter {
    protected List<Poker> pokers;

    public BaseGroupFilter(List<Poker> pokers) {
        this.pokers = pokers;
    }

    @Override
    public boolean filter() {
        Collections.sort(pokers);
        return filterHandler();
    }

    protected abstract boolean filterHandler();

    protected Map<Poker.TYPE_ENUM, Integer> groupType(List<Poker> pokers) {
        Map<Poker.TYPE_ENUM, Integer> result = new HashMap<>();
        for (Poker poker : pokers) {
            result.put(poker.getType(), result.get(poker.getType()) == null ? 1 : result.get(poker.getType()) + 1);
        }
        return result;
    }

    protected Map<Integer, Integer> groupNum(List<Poker> pokers) {
        Map<Integer, Integer> result = new HashMap<>();
        for (Poker poker : pokers) {
            result.put(poker.getNum(), result.get(poker.getNum()) == null ? 1 : result.get(poker.getNum()) + 1);
        }
        return result;
    }
}
